<?php
namespace App\Http\Controllers\Admin;

use App\Models\Admin;

/**
 * 后台首页控制器
 */
class AdminController extends BaseController
{

    public function __construct(Admin $admin)
    {
        parent::__construct();
        $this->admin = $admin;
    }

    public function getIndex()
    {
        $rows = $this->admin->getAdminList();
        return view('admin.admin.index', compact('rows'));
    }

    public function getAdd()
    {
        return view('admin.admin.add');
    }

    public function postAdd()
    {
        $res = $this->admin->add();
        if (!$res) {
            return $this->error($this->admin->getError());
        }
        return $this->success($res, urlAdmin('admin', 'getIndex'));
    }


    /**
     * 修改昵称
     */
    public function getEditNickname()
    {
        return view('admin.admin.edit_nickname');
    }

    /**
     * 修改昵称提交
     */
    public function postEditNickname()
    {
        $res = $this->admin->editNickname();
        if (!$res) {
            return $this->error($this->admin->getError());
        }
        return $this->success($res);
    }

    /**
     * 修改密码初始化
     */
    public function getEditPassword()
    {
        return view('admin.admin.edit_password');
    }

    /**
     * 修改密码提交
     */
    public function postEditPassword()
    {
        $res = $this->admin->editPassword();
        if (!$res) {
            return $this->error($this->admin->getError());
        }
        return $this->success($res);
    }

    /**
     * 管理员状态修改
     */
    public function anyChangeStatus()
    {
        $method = request('method');
        $ids = array_unique((array)request('ids'));
        if (in_array(C('USER_ADMINISTRATOR'), $ids)) {
            return $this->error("不允许对超级管理员执行该操作!");
        }
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['id'] = ['in', $ids];
        switch (strtolower($method)) {
            case 'forbid':
                return $this->forbid($this->admin, $where);
            case 'resume':
                return $this->resume($this->admin, $where);
            case 'delete':
                return $this->delete($this->admin, $where);
            default:
                return $this->error('参数非法');
        }
    }

}
